package com.cskaoyan.service;

import com.cskaoyan.bean.User;
import com.cskaoyan.bean.UserData;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.po.MarketUserExample;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * @author stone
 * @date 2022/01/06 16:36
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    MarketUserMapper marketUserMapper;

    @Override
    public UserData query(BaseParam param, String username, String mobile, Integer userId) {
        // 在执行查询之前，可以开启分页
        // sql语句预编译的过程给你拼接上分页的sql
        PageHelper.startPage(param.getPage(), param.getLimit());
//        // 使用的userMapper中并没有提供page和limit
//        List<User> users = userMapper.select(param.getSort(), param.getOrder(), "%" + username + "%");
        MarketUserExample example = new MarketUserExample();
        MarketUserExample.Criteria criteria = example.createCriteria();
        //添加条件
        if (!StringUtils.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtils.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        if (userId != null) {
            criteria.andIdEqualTo(userId);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketUser> marketUsers = marketUserMapper.selectByExample(example);
        // 会去获得一些分页信息
        // 上面的查询对应的PageInfo → 把原始查询结果放入到构造方法里
        PageInfo pageInfo = new PageInfo(marketUsers);

        // total是总的数据量 → 是否等于users.length?不是 → 指的是如果不分页的情况下最多会查询出来多少条记录
        // 按照上面的查询的查询条件执行的select count(*)
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return UserData.data(total, pages, param.getLimit(), param.getPage(), marketUsers);
    }

    @Override
    public User queryNicknameAndAvatarById(Integer userId) {
         User user = userMapper.selectNicknameAndAvatarByPrimaryKey(userId);
        return user;
    }

    @Override
    public MarketUser queryById(Integer id) {
        MarketUser marketUser = marketUserMapper.selectByPrimaryKey(id);
        return marketUser;
    }

    @Override
    public void update(MarketUser marketUser) {
        MarketUserExample marketUserExample = new MarketUserExample();
        //找到要修改的数据
        MarketUserExample.Criteria criteria = marketUserExample.createCriteria();
        criteria.andIdEqualTo(marketUser.getId());
        MarketUser newUser = new MarketUser();
        newUser.setNickname(marketUser.getNickname());
        newUser.setPassword(marketUser.getPassword());
        newUser.setMobile(marketUser.getMobile());
        newUser.setGender(marketUser.getGender());
        newUser.setUserLevel(marketUser.getUserLevel());
        newUser.setStatus(marketUser.getStatus());
        marketUserMapper.updateByExampleSelective(newUser,marketUserExample);
    }
}
